package cn.jbolt.ndemo.service.impl;

import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.jbolt.ndemo.dao.ComplaintPushMapper;
import cn.jbolt.ndemo.entity.ComplaintPush;
import cn.jbolt.ndemo.service.ComplaintPushService;
import cn.jbolt.util.Result;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class ComplaintPushServiceImpl extends ServiceImpl<ComplaintPushMapper, ComplaintPush> implements ComplaintPushService {

    //固定写死部门id
    private static final Long DEPT_ID_TECHNOLOGY = 1L;//技术部门id
    private static final Long DEPT_ID_MARKET = 2L;//市场部
    private static final Long DEPT_ID_CUSTOMER_SERVICE = 3L;//客服部

    @Override
    public Result getList(Long deptId, String keywords) {
        QueryWrapper<ComplaintPush> queryWrapper = new QueryWrapper();
        if (deptId != null) {
            //根据部门id查询
            queryWrapper.eq("dept_id", deptId);
        }
        if (StrUtil.isNotEmpty(keywords)) {
            //根据关键字查询
            queryWrapper.and(wrapper -> wrapper.like("content", keywords).or().like("title", keywords));
        }
        queryWrapper.orderByDesc("id");
        queryWrapper.last(" limit  15");
        return Result.success(this.list(queryWrapper));
    }

    @Override
    public Result add(ComplaintPush complaintPush) {
        complaintPush.setSn(RandomUtil.randomString(4) + System.currentTimeMillis());
        boolean save = this.save(complaintPush);
        if (save) {
            return Result.success();
        }
        return Result.error("新建失败");
    }

    @Override
    public Result getCount() {
        Map<String, Integer> map = baseMapper.getCount(DEPT_ID_TECHNOLOGY, DEPT_ID_MARKET, DEPT_ID_CUSTOMER_SERVICE).get(0);
        return Result.success(map);
    }
}
